﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlSerialOrdersProcess
    {
        /// <summary>
        /// 9.0通过Serial获取出货单信息和库存信息
        /// DataSource:Orders
        /// </summary>

        public static string GetPDASerialOrdersData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string serial = string.Empty;
            string orderkey = string.Empty;
            string sku = string.Empty;
            string batch = string.Empty;
            string qty = string.Empty;
            string qty_inv = string.Empty;
            string lpn = string.Empty;
            string loc = string.Empty;
            string return_status = "";
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            string strSql1 = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("orders");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;

                userid = nodeSku.SelectSingleNode("userid").InnerText;
                whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                serial = nodeSku.SelectSingleNode("serial").InnerText;

                //通过Serial查询拣货明细
                strSql = "SELECT  P.whseid,P.pickdetailkey,P.line_no,P.orderkey,P.sku,P.qty,P.fromloc,P.lot,P.lpn,P.lottable01,P.status,L.udf1  ";
                strSql += " FROM PickDetail AS P";
                strSql += " LEFT JOIN LotAttribute AS L ON L.lot=P.lot AND L.whseid=P.whseid AND L.sku=P.sku";
                strSql += " WHERE L.udf1 ='"+serial+"' AND P.whseid='"+whseid+"' AND P.status='01'";
                DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "PickDetail");
                //通过Serial查询库存
                strSql1 = "SELECT lpn,loc,qty,lottable01  FROM v_newInventory WHERE udf1='" + serial + "'  and whseid='" + whseid + "' ";
                DataTable dt_inv = App.GetDataTable(App.DBconWMS, strSql1, "inv");
                #endregion

                if (dt.Rows.Count > 0 && dt_inv.Rows.Count > 0)
                {
                    #region
                    orderkey = dt.Rows[0]["orderkey"].ToString();
                    sku = dt.Rows[0]["sku"].ToString();
                    qty= dt.Rows[0]["qty"].ToString().Substring(0, dt.Rows[0]["qty"].ToString().Length - 3);
                    batch = dt.Rows[0]["lottable01"].ToString();
                    lpn = dt.Rows[0]["lpn"].ToString();
                    loc = dt.Rows[0]["fromloc"].ToString();
                    batch= dt.Rows[0]["lottable01"].ToString();
                    qty_inv = dt_inv.Rows[0]["qty"].ToString().Substring(0, dt_inv.Rows[0]["qty"].ToString().Length-3);
                    if (qty != qty_inv)
                    {
                        return_status += "出货数量和库存数量不符--无法继续，请检查！";
                    }
                    #endregion

                    #region 构建XML报文
                    XmlNode orders_result = docreturn.CreateElement("orders_return");

                    XmlElement useridResult = docreturn.CreateElement("userid");
                    useridResult.InnerText = userid;
                    orders_result.AppendChild(useridResult);

                    XmlElement whseidResult = docreturn.CreateElement("whseid");
                    whseidResult.InnerText = whseid;
                    orders_result.AppendChild(whseidResult);

                    XmlElement serialResult = docreturn.CreateElement("serial");
                    serialResult.InnerText = serial;
                    orders_result.AppendChild(serialResult);

                    XmlElement orderResult = docreturn.CreateElement("orderkey");
                    orderResult.InnerText = orderkey;
                    orders_result.AppendChild(orderResult);

                    XmlElement skuResult = docreturn.CreateElement("sku");
                    skuResult.InnerText = sku;
                    orders_result.AppendChild(skuResult);

                    XmlElement qtyResult = docreturn.CreateElement("qty");
                    qtyResult.InnerText = qty;
                    orders_result.AppendChild(qtyResult);

                    XmlElement lpnResult = docreturn.CreateElement("lpn");
                    lpnResult.InnerText = lpn;
                    orders_result.AppendChild(lpnResult);

                    XmlElement locResult = docreturn.CreateElement("loc");
                    locResult.InnerText = loc;
                    orders_result.AppendChild(locResult);

                    XmlElement batchResult = docreturn.CreateElement("batch");
                    batchResult.InnerText = batch;
                    orders_result.AppendChild(batchResult);

                    XmlElement qty_invResult = docreturn.CreateElement("qty_inv");
                    qty_invResult.InnerText = qty_inv;
                    orders_result.AppendChild(qty_invResult);

                    XmlElement return_statusResult = docreturn.CreateElement("return_status");
                    return_statusResult.InnerText = return_status;
                    orders_result.AppendChild(return_statusResult);

                    rootResult.AppendChild(orders_result);
                    #endregion
                }
                else
                {
                    #region
                    if (dt.Rows.Count == 0)
                    {
                        return_status = "Serial：" + serial + "在拣货单中不存在！";
                    }
                    if (dt_inv.Rows.Count == 0)
                    {
                        return_status += "  Serial：" + serial + "在库存中不存在！";
                    }
                    #endregion

                    #region 构建XML报文
                    XmlNode orders_result = docreturn.CreateElement("orders_return");

                    XmlElement useridResult = docreturn.CreateElement("userid");
                    useridResult.InnerText = userid;
                    orders_result.AppendChild(useridResult);

                    XmlElement whseidResult = docreturn.CreateElement("whseid");
                    whseidResult.InnerText = whseid;
                    orders_result.AppendChild(whseidResult);

                    XmlElement serialResult = docreturn.CreateElement("serial");
                    serialResult.InnerText = serial;
                    orders_result.AppendChild(serialResult);

                    XmlElement orderResult = docreturn.CreateElement("orderkey");
                    orderResult.InnerText = orderkey;
                    orders_result.AppendChild(orderResult);

                    XmlElement skuResult = docreturn.CreateElement("sku");
                    skuResult.InnerText = sku;
                    orders_result.AppendChild(skuResult);

                    XmlElement qtyResult = docreturn.CreateElement("qty");
                    qtyResult.InnerText = qty;
                    orders_result.AppendChild(qtyResult);

                    XmlElement lpnResult = docreturn.CreateElement("lpn");
                    lpnResult.InnerText = lpn;
                    orders_result.AppendChild(lpnResult);

                    XmlElement locResult = docreturn.CreateElement("loc");
                    locResult.InnerText = loc;
                    orders_result.AppendChild(locResult);

                    XmlElement batchResult = docreturn.CreateElement("batch");
                    batchResult.InnerText = batch;
                    orders_result.AppendChild(batchResult);

                    XmlElement qty_invResult = docreturn.CreateElement("qty_inv");
                    qty_invResult.InnerText = qty_inv;
                    orders_result.AppendChild(qty_invResult);

                    XmlElement return_statusResult = docreturn.CreateElement("return_status");
                    return_statusResult.InnerText = return_status;
                    orders_result.AppendChild(return_statusResult);

                    rootResult.AppendChild(orders_result);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlSerialOrdersProcess.cs-1", ex.Message.ToString());
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }
    }
}
